function get_suffix(filename) {
	var pos = filename.lastIndexOf('.')
	var suffix = ''
	if (pos != -1) {
		suffix = filename.substring(pos)
	}
	return suffix;
}

function random_string(len) {
	len = len || 32;
	var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
	var maxPos = chars.length;
	var pwd = '';
	for (var i = 0; i < len; i++) {
		pwd += chars.charAt(Math.floor(Math.random() * maxPos));
	}
	return pwd;
}

const prePage = () => {
	let pages = getCurrentPages();
	let prePage = pages[pages.length - 2];
	
	// #ifdef MP-WEIXIN
	return prePage.$vm;
	// #endif
	return prePage;
}

const msg = (title, duration = 3000, mask = false, icon = 'none') => {
	//统一提示方便全局修改
	if (Boolean(title) === false) {
		return;
	}
	uni.showToast({
		title,
		duration,
		mask,
		icon
	});
}

//计算指定时间到当前时间剩余天数
const dateDiff = (edate) => {
	let date1 = new Date(date);
	let date2 = new Date();
	let iDays = parseInt(Math.abs(date1.getTime() - date2.getTime()) / 1000 / 60 / 60 / 24);
	return iDays;
}

let userInfo = undefined

const logout = () => {
	userInfo = undefined
	uni.removeStorage({
		key: 'userInfo'
	})
	uni.removeStorage({
		key: 'token'
	})
}

const setUserInfo = (i) => {
	userInfo = i
}

const setLinkUrl = (url) => {
	let newLinkUrl = '';
	let token = uni.getStorageSync('token') || '';
	if(url.indexOf('?') != -1){
		newLinkUrl = url + '&stoken=' + token;
	}else{
		newLinkUrl = url + '?stoken=' + token;
	}
	return newLinkUrl;
}

const resolvingDate = (date) => {
	if(date!=null){
		let d = new Date(date.replace(/-/g,'/'));
		let hours = d.getHours() < 10 ? '0' + d.getHours() : d.getHours();
		let min = d.getMinutes() < 10 ? '0' + d.getMinutes() : d.getMinutes();
		let times = hours + ':' + min;
		return times;
	}
}

function dateDiff2(date) {
	// let date = '2020-11-22 12:34:03'
	let oldDate = new Date(date.replace(/-/g,'/'));
	let nowDate = new Date();
	let oneDay = 24 * 60 * 60 * 1000
	let times = null
  if(oldDate === nowDate){
	  times='今天' + oldDate.getHours() + ':' + oldDate.getMinutes()
  }else if(oldDate >= (nowDate - oneDay) && oldDate  < nowDate){
	  times="昨天" + oldDate.getHours() + ':' + oldDate.getMinutes()
  }else {
	  times = (oldDate.getMonth() + 1) + '月' + oldDate.getDay()+ '日'
  }
  return times;
}

//系统访问路径
const SYS_URL = 'https://wx.hzcl.org.cn'

//oss图片路径
const IMG_URL = '/images/aid-apps-mui/'

export default {
	prePage,
	msg,
	logout,
	setUserInfo,
	dateDiff,
	dateDiff2,
	setLinkUrl,
	resolvingDate,
	SYS_URL,
	IMG_URL
}

